#!/bin/bash

# 用法: ./changeFtpPasswd.sh <username> <new_password> [server_ip]


NEW_PASSWD="$1"
USERNAME="ftpuser"
SERVER_IP="192.168.3.81"

# USERNAME="$1"
# NEW_PASSWD="$2"
# SERVER_IP="${3:-localhost}"

if [ -z "$USERNAME" ] || [ -z "$NEW_PASSWD" ]; then
  #echo "Usage: $0 <username> <new_password> [server_ip]"
  exit -1
fi

# echo "Connecting to $SERVER_IP to change password for user '$USERNAME'..."

ssh "$SERVER_IP" "echo \"$USERNAME:$NEW_PASSWD\" | sudo chpasswd" 2>&1

if [ $? -eq 0 ]; then
  # echo "Password for FTP user '$USERNAME' changed successfully on $SERVER_IP."
  exit 0;
else
  # echo "Failed to change password for FTP user '$USERNAME' on $SERVER_IP."
  exit -2;
fi
